﻿using HarmonyLib;
using RimWorld;
using System;
using System.Collections.Generic;
using UnityEngine;
using Verse;
using GiddyUp;

using static GiddyUp.IsMountableUtility;

using Settings = GiddyUp.ModSettings_GiddyUp;
using System.Reflection;

namespace Meow
{
    public class MountableHumanoid : DefModExtension { }

    public static class GiddyUpExts
    {

        public static ExtendedPawnData GetGUData(this Pawn pawn)
        {
            //return ExtendedDataStorage.GUComp[pawn.thingIDNumber];
            var _store = ExtendedDataStorage.GUComp._store;
            int pawnID = pawn?.thingIDNumber ?? -1;
            if (_store.TryGetValue(pawnID, out ExtendedPawnData data))
            {
                return data;
            }
            else if (pawnID == -1) Log.Warning("[Giddy-Up] Invalid pawnID sent.");

            var newExtendedData = new ExtendedPawnData(pawn);

            _store[pawnID] = newExtendedData;
            return newExtendedData;
        }

        public static bool GenerateFloatMenuOption(this List<FloatMenuOption> list, string text, bool prefixType = false, Action action = null)
        {
            if (!prefixType) text = "GUC_CannotMount".Translate() + text;
            list.Add(new FloatMenuOption(text, action, MenuOptionPriority.Low));
            return true;
        }
    }
    public class EponaGiddyUp2Patch
    {

        [HarmonyPatch(typeof(IsMountableUtility), nameof(IsMountableUtility.IsMountable))]
        public class IsMountablePatch
        {
            public static Intelligence temp = Intelligence.Humanlike;
            [HarmonyPrefix]
            public static void Prefix(ref Pawn animal)
            {
                Debug.Log("IsMountablePatch Prefix");
                if (animal != null && animal.def.modExtensions.Any(t => t is MountableHumanoid))
                {
                    Debug.Log("MountableHumanoid- Prefix");

                    temp = animal.RaceProps.intelligence;
                    animal.RaceProps.intelligence = Intelligence.Animal; // temp set
                }
            }
            [HarmonyPostfix]
            public static void Postfix(ref Pawn animal)
            {
                Debug.Log("IsMountablePatch Postfix");

                if (animal != null && animal.def.modExtensions.Any(t => t is MountableHumanoid))
                {
                    Debug.Log("MountableHumanoid- Postfix");

                    animal.RaceProps.intelligence = temp; // temp set
                }
            }

        }


        [HarmonyPatch(typeof(FloatMenuMakerMap), "AddHumanlikeOrders", new Type[] { typeof(Vector3), typeof(Pawn), typeof(List<FloatMenuOption>) })]
        static class Patch_AddDraftedOrders
        {

            static void Postfix(Vector3 clickPos, Pawn pawn, List<FloatMenuOption> opts)
            {
                foreach (LocalTargetInfo current in GenUI.TargetsAt(clickPos, TargetingParameters.ForPawns(), true))
                {
                    if (current.Thing is Pawn target)
                    {
                        var assembly = typeof(GiddyUp.CanDoMounted).Assembly;
                        // 获取私有类型
                        Type privateType = assembly.GetType("GiddyUp.Harmony.FloatMenuUtility");
                        //object instance = Activator.CreateInstance(privateType, nonPublic: true);

                        MethodInfo staticPrivateMethod = privateType.GetMethod(
                            "AddMountingOptions",
                            BindingFlags.Public | BindingFlags.Static
                        );

                        if (staticPrivateMethod == null)
                        {
                            throw new MissingMethodException("找不到静态私有方法");
                        }

                        object result = staticPrivateMethod.Invoke(null, new object[] { target, pawn, opts });

                        //MethodInfo privateMethod = privateType.GetMethod("AddMountingOptions", BindingFlags.NonPublic | BindingFlags.Instance);
                        //privateMethod.Invoke(instance, new object[] { target, pawn, opts });
                    }

                }
            }
        }
    }


}
